package net.sky.oa.model;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import net.sky.oa.util.ReflectionUtils;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
/**
 * 工作总结表
 * OffSummary. @author hond
 */
@Entity
@Table(name = "OFF_SUMMARY")//工作总结表
public class OffSummary implements java.io.Serializable {

	private static final long serialVersionUID = 5673991033245515351L;
	
	// Fields
	protected Long id;
	private String title;    //总结标题
	private String content;  //总结内容
	private String annexurl; //附件地址
	private Integer type;       //总结类型
	private Integer sort;       //总结类别
	private Date lasttime;   //最后修改时间
	private Integer ispublic;//是否公开：1是2否
	private Integer status;     //状态 : 1为正常,2为已删除
	private SysEmployee user;  //用户id
	private SysDept dept;    //部门id

	private Set<FileAttach> summFiles;  //总结附件
	private Set<OffSummUser> offSummUsers; //发送用户
	

	/** default constructor */
	public OffSummary() {
	}
	
	
	
	/**
	 * @param id
	 * @param title
	 * @param content
	 * @param annexurl
	 * @param type
	 * @param sort
	 * @param lasttime
	 * @param ispublic
	 * @param status
	 * @param user
	 * @param dept
	 * @param offSummUserNames
	 * @param offSummUserIds
	 * @param summFiles
	 * @param offSummUsers
	 */
	public OffSummary(Long id, String title, String content, String annexurl,
			Integer type, Integer sort, Date lasttime, Integer ispublic,
			Integer status, SysEmployee user, SysDept dept,
			String offSummUserNames, String offSummUserIds,
			Set<FileAttach> summFiles, Set<OffSummUser> offSummUsers) {
		super();
		this.id = id;
		this.title = title;
		this.content = content;
		this.annexurl = annexurl;
		this.type = type;
		this.sort = sort;
		this.lasttime = lasttime;
		this.ispublic = ispublic;
		this.status = status;
		this.user = user;
		this.dept = dept;
//		this.offSummUserNames = offSummUserNames;
//		this.offSummUserIds = offSummUserIds;
		this.summFiles = summFiles;
		this.offSummUsers = offSummUsers;
	}



	@Id
	@Column(name = "ID", unique = true, nullable = false)
	@SequenceGenerator(name="SQ_OFFSUMMARY_ID",sequenceName="SQ_OFFSUMMARY_ID",allocationSize=1)
    @GeneratedValue(strategy=GenerationType.SEQUENCE,generator="SQ_OFFSUMMARY_ID")
	public Long getId() {
	    return id;
	}

    public void setId(Long id){
	    this.id = id;
	}
	@Column(name = "TITLE", length = 60)
	public String getTitle() {
		return this.title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	@Column(name = "CONTENT")
	public String getContent() {
		return this.content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	@Column(name = "ANNEXURL", length = 100)
	public String getAnnexurl() {
		return this.annexurl;
	}

	public void setAnnexurl(String annexurl) {
		this.annexurl = annexurl;
	}

	@Column(name = "TYPE", precision = 2, scale = 0)
	public Integer getType() {
		return this.type;
	}

	public void setType(Integer type) {
		this.type = type;
	}

	@Column(name = "SORT", precision = 2, scale = 0)
	public Integer getSort() {
		return this.sort;
	}

	public void setSort(Integer sort) {
		this.sort = sort;
	}

	@Temporal(TemporalType.DATE)
	@Column(name = "LASTTIME", length = 7)
	public Date getLasttime() {
		return this.lasttime;
	}

	public void setLasttime(Date lasttime) {
		this.lasttime = lasttime;
	}

	@Column(name = "ISPUBLIC", precision = 1, scale = 0)
	public Integer getIspublic() {
		return this.ispublic;
	}

	public void setIspublic(Integer ispublic) {
		this.ispublic = ispublic;
	}

	@Column(name = "STATUS", precision = 2, scale = 0)
	public Integer getStatus() {
		return this.status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "USER_ID")
	public SysEmployee getUser() {
		return this.user;
	}

	public void setUser(SysEmployee user) {
		this.user = user;
	}

	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "DEPT_ID")
	public SysDept getDept() {
		return this.dept;
	}
	
	public void setDept(SysDept dept) {
		this.dept = dept;
	}
	 
	@ManyToMany(fetch=FetchType.EAGER)
	@JoinTable(name="summary_file",joinColumns={@JoinColumn(name="summ_id")},inverseJoinColumns={@JoinColumn(name="file_id")})  
	@Cascade(value={CascadeType.PERSIST,CascadeType.MERGE}) 
	public Set<FileAttach> getSummFiles() {
		return summFiles;
	}

	public void setSummFiles(Set<FileAttach> summFiles) {
		this.summFiles = summFiles;
	}

	@OneToMany(fetch=FetchType.EAGER,mappedBy="summ")
	@Cascade(value={CascadeType.SAVE_UPDATE,CascadeType.DELETE_ORPHAN}) 
	public Set<OffSummUser> getOffSummUsers() {
		return offSummUsers;
	}

	public void setOffSummUsers(Set<OffSummUser> offSummUsers) {
		this.offSummUsers = offSummUsers;
	}
	
	@Transient
	public Set<SysEmployee> getSummEmployees() {
		Set<SysEmployee> es = new HashSet<SysEmployee>();
		for(OffSummUser su : offSummUsers){
			es.add(su.getRusr());
		}
		return es;
	}
	

	/**
	 * 作用：
	 * 1、把原来选中现在选中的保留
	 * 2、把原来选中现在没有选中的删除掉
	 * 3、把原来没有选中现在选中的增加进去
	 * @param checkedIds
	 */
	@Transient
	public void mergeByCheckedIds(final Collection<Long> checkedIds) {
		
		Set<OffSummUser> summUsers = this.getOffSummUsers();
		
		if(summUsers == null){
			summUsers = new HashSet<OffSummUser>();
		}

		//目标集合为空, 删除源集合中所有对象后直接返回.
		if (checkedIds == null) {
			summUsers.clear();
			return;
		}else{
			Set<Long> set=new HashSet<Long>();
			List<Long> list=new ArrayList<Long>();
			for(Iterator<Long> iter=checkedIds.iterator();iter.hasNext();){
				Long id=iter.next();
				if(set.add(id)){
					list.add(id);
				}
			}
			checkedIds.clear();
			checkedIds.addAll(list);//去除checkedIds中的重复记录
		}
		
		//遍历源对象集合,如果其id不在目标ID集合中的对象删除.
		//同时,在目标集合中删除已在源集合中的id,使得目标集合中剩下的id均为源集合中没有的id.
		Iterator<OffSummUser> it = summUsers.iterator();
		try {

			while (it.hasNext()) {
				OffSummUser element = it.next();
				Long id = element.getRusr().getId();

				if (!checkedIds.contains(id)) {
					it.remove();
				} else {
					checkedIds.remove(id);
				}
			}

			//ID集合目前剩余的id均不在源集合中,创建对象,为id属性赋值并添加到源集合中.
			for (Long id : checkedIds) {
				OffSummUser summUser = new OffSummUser();
				
				SysEmployee user = new SysEmployee();
				user.setId(id);		
				summUser.setRusr(user);
				summUser.setSumm(this);
				summUser.setIsread(1);
				summUser.setSendtime(new Date());
				
				summUsers.add(summUser);
			}
			this.setOffSummUsers(summUsers);
		} catch (Exception e) {
			e.printStackTrace();
			throw ReflectionUtils.convertReflectionExceptionToUnchecked(e);
		}
	}
}